package top.gangster.lunan.contractservice.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.gangster.lunan.contractservice.entity.ContractState;
import top.gangster.lunan.contractservice.service.ContractStateService;
import top.gangster.lunan.result.Result;

@RestController
@RequestMapping("/contractservice/state")
public class ContractStateController {
	@Autowired
	ContractStateService contractStateService;

	/**
	 * 获取所有合同状态
	 * @return stateList
	 */
	@GetMapping
	public Result getAllContractState() {
		List<ContractState> stateList = contractStateService.list(null);
		return Result.ok().data("states",stateList);
	}

	/**
	 * 获取指定id的合同状态
	 * @param stateId 合同状态id
	 * @return 合同状态对象
	 */
	@GetMapping("/{stateId}")
	public Result getContractStateById(@PathVariable Integer stateId) {
		ContractState state = contractStateService.getById(stateId);
		return Result.ok().data("state",state);
	}

	/**
	 * 添加合同状态
	 * @param state 合同状态
	 * @return Result
	 */
	@PostMapping
	public Result saveContractState(@RequestBody ContractState state) {
		boolean b = contractStateService.save(state);
		if (b) {
			return Result.ok().message("添加合同状态成功");
		}
		return Result.error().message("添加合同状态失败");
	}

	/**
	 * 更新合同状态信息
	 * @param state 合同状态
	 * @return Result
	 */
	@PutMapping
	public Result updateContractState(@RequestBody ContractState state) {
		boolean b = contractStateService.updateById(state);
		if (b) {
			return Result.ok().message("更新合同状态信息成功");
		}
		return Result.error().message("更新合同状态信息失败");
	}

	/**
	 * <h2>合同状态在一般情况下不会被删除！！！</h2>
	 * 删除合同状态
	 * @param stateId 合同状态id
	 * @return Result
	 */
	@DeleteMapping("/{stateId}")
	public Result deleteContractState(@PathVariable Integer stateId) {
		boolean b = contractStateService.removeById(stateId);
		if (b) {
			return Result.ok().message("删除合同状态成功");
		}
		return Result.error().message("删除合同状态失败");
	}
}
